from PyQt5.QtCore import *
from PyQt5.QtGui import QFocusEvent
from examples.example_calculator.calc_conf import *
#from calc_conf import *#--
from examples.example_calculator.calc_node_base import *
#from calc_node_base import *#--
from nodeeditor.utils import dumpException


class CalcGraphicsNode(QDMGraphicsNode):#节点外壳
    def initSizes(self):
        super().initSizes()
        self.width = 400
        self.height = 110
        self.edge_roundness = 6
        self.edge_padding = 0
        self.title_horizontal_padding = 8
        self.title_vertical_padding = 10

    def initAssets(self):
        super().initAssets()
        self.icons = QImage("icons/status_icons.png")

    def paint(self, painter, QStyleOptionGraphicsItem, widget=None):
        super().paint(painter, QStyleOptionGraphicsItem, widget)

        offset = 24.0
        if self.node.isDirty(): offset = 0.0
        if self.node.isInvalid(): offset = 48.0

        painter.drawImage(
            QRectF(-10, -10, 24.0, 24.0),
            self.icons,
            QRectF(offset, 0, 24.0, 24.0)
        )


class CalcShuChuContent(QDMNodeContentWidget):
    def initUI(self):
        self.layout = QVBoxLayout()
        self.layout.setContentsMargins(10,0,10,0)
        self.setLayout(self.layout)
        
        self.edit = QTextEdit("#请输入自定义代码", self)
        self.edit.setAlignment(Qt.AlignLeft)
        self.edit.setObjectName(self.node.content_label_objname)
        self.edit.setMaximumSize(QSize(400,70))
        
        self.layout.addWidget(self.edit)
    
    def serialize(self):
        res = super().serialize()
        res['value'] = self.edit.toPlainText()
        return res

    def deserialize(self, data, hashmap={}):
        res = super().deserialize(data, hashmap)
        try:
            value = data['value']
            self.edit.setText(value)
            return True & res
        except Exception as e:
            dumpException(e)
        return res

@register_node(OP_NODE_CUSTOM)
class CalcNode_Custom(CalcNode):
    icon = "C:\\Users\\26593\\Desktop\\Seer2_15\\List\\SeerNode\\examples\\example_calculator\\icons\\in.png"
    op_code = OP_NODE_CUSTOM
    op_title = "自定义"
    content_label_objname = "calc_node_custom"
    GraphicsNode_class = CalcGraphicsNode

    def __init__(self, scene):
        super().__init__(scene, inputs=[], outputs=[1])
        self.eval()

    def initInnerClasses(self):
        self.content = CalcShuChuContent(self)
        self.grNode = CalcGraphicsNode(self)
        self.content.edit.textChanged.connect(self.onInputChanged)
    
    def onInputChanged(self, socket=None):
        super().onInputChanged(socket)
        e_height = self.content.edit.document().size().height()
        if(e_height <= 76):
            self.grNode.height = 76 + 30 + 5
            self.content.edit.setMaximumSize(QSize(400,70))
        else:
            self.grNode.height = e_height + 30
            self.content.edit.setMaximumSize(QSize(1000,1000))
        self.grNode.initContent()
        #self.initSockets(inputs=[], outputs=[1])
    
    def evalImplementation(self):
        s_value = self.content.edit.toPlainText()
        self.value = s_value
        self.markDirty(False)
        self.markInvalid(False)

        self.markDescendantsInvalid(False)
        self.markDescendantsDirty()

        self.grNode.setToolTip("")

        self.evalChildren()

        return self.value
